package com.goldgov.conference.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.conference.dao.ConferenceAttendanceDao;
import com.goldgov.conference.dao.ConferenceUserDao;
import com.goldgov.conference.service.ConferenceAttendance;
import com.goldgov.conference.service.ConferenceBean;
import com.goldgov.conference.service.ConferenceQuery;
import com.goldgov.conference.service.ConferenceUser;
import com.goldgov.conference.service.ConferenceUserQuery;
import com.goldgov.conference.service.ConferenceUserService;

/** 
 * @ClassName: ConferenceUserServiceImpl 
 * @Description: TODO
 * @author hanxiangyu
 * @date 2018年6月7日 上午11:42:08 
 */
@Service
public class ConferenceUserServiceImpl implements ConferenceUserService {

	@Autowired
	private ConferenceUserDao conferenceUserDao;
	@Autowired
	private ConferenceAttendanceDao conferenceAttendanceDao;
	 
	/** 
	 * @Description: 回执信息操作
	 * @author hanxiangyu
	 * @date 2018年6月7日 下午12:05:08 
	 */
	@Override
	public Boolean updateReceipt(ConferenceUser conferenceUser) {
		Boolean fals = false;
		int state = conferenceUserDao.updateReceipt(conferenceUser);
		if (state >= 1) {
			fals = true;
		} else {
			fals = false;
		}
		return fals;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月7日 下午12:51:06 
	 */
	@Override
	public ConferenceUser getConferenceUser(ConferenceUser conferenceUser) {
		return conferenceUserDao.getConferenceUser(conferenceUser);
	}
	public List<ConferenceUser> getConferenceUserToAtt(ConferenceUser conferenceUser){
		return conferenceUserDao.getConferenceUserToAtt(conferenceUser);
	}
	@Override
	public List<ConferenceUser> getConferenceUserQjList(String conferenceID) {
		return conferenceUserDao.getConferenceUserQjList(conferenceID);
	}

	@Override
	public List<ConferenceUser> getConferenceUserQxList(String conferenceID) {
		return conferenceUserDao.getConferenceUserQxList(conferenceID);
	}

	@Override
	public List<ConferenceUser> getConferenceUserList(ConferenceUserQuery query) {
		return conferenceUserDao.getConferenceUserList(query);
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月12日 下午7:51:26 
	 */
	@Override
	public List<String> getConferenceUserIds(ConferenceUser conferenceUser) {
		return conferenceUserDao.getConferenceUserIds(conferenceUser);
	}

	/** 
	 * @Description: 删除人员
	 */
	@Override
	public Boolean deleteConferenceUserByID(String[] ids) {
		Boolean fals = false;
		int state = conferenceUserDao.deleteConferenceUserByID(ids);
		if (state >= 1) {
			fals = true;
		}else {
			fals = false;
		}
		return fals;
		
	}

	@Override
	public List<ConferenceUser> getConferenceUserCxList(String conferenceID) {
		return conferenceUserDao.getConferenceUserCxList(conferenceID);
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月28日 上午11:00:15 
	 */
	@Override
	public List<ConferenceUser> getConferenceUserQdList(String conferenceID) {
		return conferenceUserDao.getConferenceUserQdList(conferenceID);
	}

	@Override
	public List<ConferenceUser> getUserList(String conferenceID) {
		return conferenceUserDao.getUserList(conferenceID);
	}
	public Boolean updateWaitType(ConferenceBean obj) {
		Boolean fals = false;
		int state  = 0;
		
		if (obj.getSearchType().intValue() == ConferenceQuery.STATUS_YQ) { //1更新请假 2新增考勤记录
			ConferenceUser conUser = new ConferenceUser(); //更新为已知晓
			conUser.setReceiptType(ConferenceQuery.STATUS_NOQJ);
			conUser.setReceiptDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			conUser.setConferenceID(obj.getConferenceID());
			conUser.setUserIDs(obj.getUserIDs());
			conferenceUserDao.updateUserKQByUserid(conUser);//更新请假记录
			//新增考勤记录
			List<ConferenceAttendance> list = new ArrayList<>();
			//删除考勤记录 //把人员可能存在的考勤记录删掉
			conferenceAttendanceDao.deleteAttendanceByUserId(obj.getConferenceID(), obj.getUserIDs());
			for (String userID : obj.getUserIDs()) {//拼接添加信息list
				ConferenceAttendance ca = new ConferenceAttendance();
				ca.setAttendanceID(UUID.randomUUID().toString());
				ca.setAttendanceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ca.setAttendanceStatus(ConferenceQuery.STATUS_YQ);
				ConferenceUser conferenceUser = new ConferenceUser();
				conferenceUser.setConferenceID(obj.getConferenceID());
				conferenceUser.setUserID(userID);
				ConferenceUser cu = conferenceUserDao.getConferenceUser(conferenceUser);
				ca.setConferenceUserID(cu.getConferenceUserID());
				list.add(ca);
			}
			state = conferenceAttendanceDao.addAttendanceByUserId(list);
		}else if(obj.getSearchType().intValue() == ConferenceQuery.STATUS_WQ) {//更新请假 ,删除考勤记录
			//改为未签到是
			//删除考勤记录
			conferenceAttendanceDao.deleteAttendanceByUserId(obj.getConferenceID(), obj.getUserIDs());
			//更新请假记录
			ConferenceUser conUser = new ConferenceUser();
			conUser.setReceiptType(ConferenceQuery.STATUS_NOQJ);
			conUser.setReceiptDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			conUser.setConferenceID(obj.getConferenceID());
			conUser.setUserIDs(obj.getUserIDs());
			state = conferenceUserDao.updateUserKQByUserid(conUser);
		}else if(obj.getSearchType().intValue() == ConferenceQuery.STATUS_QJ) { //删除考勤记录,更新为请假
			//改为未签到是
			//删除考勤记录
			conferenceAttendanceDao.deleteAttendanceByUserId(obj.getConferenceID(), obj.getUserIDs());
			//更新请假记录
			ConferenceUser conUser = new ConferenceUser();
			conUser.setReceiptType(ConferenceQuery.STATUS_QJ);
			conUser.setReceiptDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			conUser.setConferenceID(obj.getConferenceID());
			conUser.setUserIDs(obj.getUserIDs());
			state = conferenceUserDao.updateUserKQByUserid(conUser);
		}
		if (state >= 1) {
			fals = true;
		}else {
			fals = false;
		}
		return fals;
	}
	
}
